package com.smooth.site.sx.timer;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;
import com.smooth.common.site.common.enums.DelFlagEnums;
import com.smooth.common.site.modules.sys.enums.SysAreaTypeEnum;
import com.smooth.dao.gen.BSysAreaMapper;
import com.smooth.dao.gen.WxProvinceMapper;
import com.smooth.dao.gen.bean.BSysArea;
import com.smooth.dao.gen.bean.WxProvince;
import com.smooth.dao.gen.bean.WxProvinceExample;

public class AreaDataMain {
	public static void main(String[] args) {
		Logger log = LoggerFactory.getLogger(AreaDataMain.class);
		try {
			String springFileXml = "classpath*:spring-context*.xml";
			log.info(springFileXml);
			ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(springFileXml);
			
			
			WxProvinceMapper wxProvinceMapper = applicationContext.getBean(WxProvinceMapper.class);
			WxProvinceExample wxProvinceExample = new WxProvinceExample();
			wxProvinceExample.setOrderByClause(" id asc ");
			List<WxProvince> wlist = wxProvinceMapper.selectByExample(wxProvinceExample);
			
			BSysAreaMapper sysAreaMapper = applicationContext.getBean(BSysAreaMapper.class);
			
			Date date = new Date();
			for(WxProvince wxProvince : wlist) {
				BSysArea bsysAreaRecord = new BSysArea();
				bsysAreaRecord.setId(wxProvince.getId()+"");
				bsysAreaRecord.setName(wxProvince.getName());
				
				bsysAreaRecord.setParentId(wxProvince.getUpid()+",");
				String parentIds = wxProvince.getUpid()+",";
				
				WxProvince wxP1Province = wxProvinceMapper.selectByPrimaryKey(wxProvince.getUpid());
				while(wxP1Province!=null) {
					parentIds = parentIds+""+wxP1Province.getUpid()+",";
					wxP1Province = wxProvinceMapper.selectByPrimaryKey(wxP1Province.getUpid());
					
				}
				bsysAreaRecord.setParentIds(parentIds);

				bsysAreaRecord.setSort(wxProvince.getId()*1l);
				SysAreaTypeEnum sysAreaTypeEnum = SysAreaTypeEnum.getStatus(wxProvince.getLevel());
				bsysAreaRecord.setType(sysAreaTypeEnum.value+"");
				bsysAreaRecord.setUpdateDate(date);
				bsysAreaRecord.setCreateDate(date);
				bsysAreaRecord.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value+"");
				bsysAreaRecord.setCreateBy(1+"");
				bsysAreaRecord.setUpdateBy(1+"");
				sysAreaMapper.insertSelective(bsysAreaRecord);
				
				log.info("插入【"+JSON.toJSONString(bsysAreaRecord)+"】成功");
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}

}
